// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false

//搭建vuex的基本环境
import Vuex from "vuex";
Vue.use(Vuex)

//创建仓库
const store = new Vuex.Store({
  strict: true,
  //state:仓库中存放的数据
  state:{
    count:0,
    firstName:"达",
    lastName:"姆"
  },
  //仓库内部的计算属性
  getters:{
    flag(state){
      return state.count % 2 === 0 ? "偶":"奇"
    },
    fullName(state){
      return `${state.firstName}-${state.lastName}`
    }
  },
  //mutations:修改仓库数据的工具
  mutations:{
    add(state,{num,text}){
      console.log(text);
      state.count+=num
    },
    desc(state,{num,text}){
      console.log(text);
      state.count-=num
    },
    asyncAdd(state,num){
      //在mutation内部 只能同步的修改数据 不能异步修改
      /*setTimeout(()=>{
        state.count += 1
      },2000)*/
      state.count += num
    }
  },
  //抒写异步逻辑的工具;
  actions:{
    asyncAdd(store,num){
      setTimeout(()=>{
        store.commit("asyncAdd",num)
      },2000)
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  components: { App },
  template: '<App/>',
  store
})
